package model

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"time"
)

var DB *sql.DB

type User struct {
	Uid   int
	Name  string
	Phone string
}

// 初始化数据库连接
func init() {
	DB, _ = sql.Open("mysql", "root:root@tcp(192.168.179.22)/beego")
	DB.SetConnMaxLifetime(time.Minute * 3)
	DB.SetMaxOpenConns(10)
	DB.SetMaxIdleConns(10)
}

// 获取用户信息
func GetUser(uid int) (u User) {
	err := DB.QueryRow("select id,name,phone from user where id = ?", uid).Scan(&u.Uid, &u.Name, &u.Phone)
	if err != nil {
		fmt.Printf("scan failed,err:%v\n", err)
		return
	}
	return u
}
